package com.ict.web.controller.yzq;


import com.github.pagehelper.PageInfo;
import com.ict.common.annotation.Log;
import com.ict.common.core.controller.BaseController;
import com.ict.common.core.domain.AjaxResult;
import com.ict.common.enums.BusinessType;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.poi.ExcelUtil;
import com.ict.web.domain.ExtractExpert;
import com.ict.web.dto.ExpertExportDto;
import com.ict.web.dto.ExpertValuateDto;
import com.ict.web.dto.ExtractExpertEditDto;
import com.ict.web.service.IExtractExpertService;
import com.ict.web.vo.ExpertValuateExport;
import com.ict.web.vo.ExpertValuateVo;
import com.ict.web.vo.ExtractExpertDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 抽取专家Controller
 *
 * @author xc
 * @date 2022-02-25
 */

@Api(tags = "抽取专家")
@RestController
@RequestMapping("/extractExpert")
public class ExtractExpertController extends BaseController
{

    @Autowired
    private IExtractExpertService extractExpertService;

    /**
     * 查询抽取专家详情
     */
    @ApiOperation("查询抽取专家详情")
    @GetMapping(value = "/{extractExpertId}")
    public ExtractExpert getExpertInfo(@PathVariable("extractExpertId") Long extractExpertId)
    {
        return extractExpertService.getById(extractExpertId);
    }

    /**
     * 批量修改抽取专家
     */
    @PutMapping("/batchUpdate/{reviewTaskId}")
    @Log(title = "批量修改抽取专家", businessType = BusinessType.UPDATE)
    @ApiOperation(value ="批量修改抽取专家")
    public AjaxResult batchUpdate(@RequestBody List<ExtractExpertEditDto> list,@PathVariable Long reviewTaskId)
    {
        return toAjax(extractExpertService.batchUpdate(list,reviewTaskId));
    }
    /**
     * 删除该专家对应的评审任务
     */
    @DeleteMapping("/deleteReviewTask/{reviewTaskId}")
    @Log(title = "删除该专家对应的评审任务", businessType = BusinessType.DELETE)
    @ApiOperation(value ="删除该专家对应的评审任务")
    public AjaxResult deleteReviewTask(@PathVariable Long reviewTaskId)
    {
        return toAjax(extractExpertService.deleteReviewTask(reviewTaskId));
    }
    /**
     * 修改抽取专家信息
     */
    @PutMapping("/update")
    @Log(title = "修改抽取专家信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value ="修改抽取专家信息")
    public AjaxResult update(@Valid @RequestBody ExtractExpertDetailVo dto)
    {
        return toAjax(extractExpertService.update(dto));
    }

    /**
     * 申请取消审核
     * @param extractExpertId
     * @param status
     * @return
     */
    @PutMapping("/approval")
    @ApiOperation(value ="修改抽取专家信息")
    public AjaxResult approval(Long extractExpertId,String status,Long reviewTaskId)
    {
        return toAjax(extractExpertService.approval(extractExpertId,status,reviewTaskId));
    }
    /**
     * 专家评价明细
     */
    @ApiOperation("专家评价明细")
    @GetMapping(value = "/getExpertValuate")
    public PageInfo getExpertValuate (ExpertValuateDto dto)
    {
        startPage();
        return new PageInfo(extractExpertService.getExpertValuate(dto));
    }
    /**
     * 专家评价明细导出
     */
    @ApiOperation("专家评价明细导出")
    @PostMapping(value = "/export")
    public void export(HttpServletResponse response, @Valid @RequestBody ExpertExportDto dto) {
        List<ExpertValuateExport> listExcels = new ArrayList<>();
        ArrayList<Object> arrayList = new ArrayList<>();
        if(StringUtils.isNotEmpty(dto.getList())){
            arrayList.addAll(dto.getList());
        }else {
            List<ExpertValuateVo> expertValuateVoList = extractExpertService.getExpertValuate(dto.getDto());
            arrayList.addAll(expertValuateVoList);
        }
        arrayList.stream().forEach(s->{
            ExpertValuateExport expertValuateExport = new ExpertValuateExport();
            BeanUtils.copyProperties(s,expertValuateExport);
            switch (expertValuateExport.getReviewType()) {
                case "0":
                    expertValuateExport.setReviewType("方案评审");
                    break;
                case "1":
                    expertValuateExport.setReviewType("年度计划评审");
                    break;
            }
            listExcels.add(expertValuateExport);
        });
        ExcelUtil<ExpertValuateExport> util = new ExcelUtil<>(ExpertValuateExport.class);
        listExcels.stream().forEach(s->{
            LocalDateTime updateTime = s.getUpdateTime();
            String replace = String.valueOf(updateTime).replace('T', ' ');
            s.setTime(replace);
        });
        util.exportExcel(response, listExcels, "专家评价明细导出");

    }
}
